package com.uex.dev.manage.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.query.MPJQueryWrapper;
import com.google.common.base.Preconditions;
import com.uex.dev.manage.admin.dao.DeviceRobotDao;
import com.uex.dev.manage.admin.po.DeviceRobotPO;
import com.uex.dev.manage.admin.service.IDeviceRobotService;
import com.uex.dev.manage.admin.utils.LogUtil;
import com.uex.dev.manage.admin.vo.device.RobotInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wangchao
 * @since 2024-02-22
 */
@Service
public class DeviceRobotServiceImpl extends ServiceImpl<DeviceRobotDao, DeviceRobotPO> implements IDeviceRobotService {

    @Override
    public RobotInfo getRobotInfo(Integer deviceId) {
        Preconditions.checkArgument(ObjectUtils.isNotNull(deviceId), "设备Id不能为空");
        RobotInfo robotInfo = this.selectJoinOne(RobotInfo.class, new MPJQueryWrapper<DeviceRobotPO>()
                .select("t.*,td.device_modelid as model_id, td.device_model")
                .leftJoin("t_device td on t.device_id = td.id")
                .eq("t.device_id", deviceId));
        Preconditions.checkArgument(ObjectUtils.isNotNull(robotInfo), "设备不存在");
        LogUtil.log(deviceId, "获取收单机信息");
        return robotInfo;
    }

    @Override
    public void updateRobot(RobotInfo robotUpdateInfoReq) {
        Preconditions.checkArgument(ObjectUtils.isNotNull(robotUpdateInfoReq.getDeviceId()), "设备Id不能为空");
        DeviceRobotPO robot = this.getById(robotUpdateInfoReq.getDeviceId());
        Preconditions.checkArgument(ObjectUtils.isNotNull(robot), "设备不存在");
        // 传值更新，不传不更新
        BeanUtils.copyProperties(robotUpdateInfoReq, robot);
        LogUtil.log(robot.getDeviceId(), "更新收单机信息");
        this.updateById(robot);
    }
}
